package com.pluser.mq.engine;

import java.io.IOException;

import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.pluser.mq.vo.QueueInfo;
import com.pluser.mq.vo.QueueManagerInfo;

/**
 * 
 * Receiver class
 * @author pluser
 * @version 1.0
 * @date 2014-05-03
 * @modifiedHis
 *
 * @description
 * Receiver class that receive message from queue
 */
public class Receiver extends AbstractHandler {

	/**
	 * constructor
	 * @param qmInfo info of queue manager
	 * @param qInfo info of queue
	 * @throws MQException
	 */
	public Receiver(QueueManagerInfo qmInfo, QueueInfo qInfo)
			throws MQException {
		super(qmInfo, qInfo);
	}
	
	@Override
	public byte[] getMsg(MQGetMessageOptions gmo) 
			throws IOException, MQException {
		
		MQMessage outMsg = new MQMessage();
		byte[] msgBytes = null;
		int len = 0;
		
		getQueue().get(outMsg, gmo);
		len = outMsg.getMessageLength();
		
		if(len > 0) {
			
			msgBytes = new byte[len];
		}
		
		outMsg.readFully(msgBytes);
		
		return msgBytes;
	}
	
	/**
	 * get message into msg with gmo get options
	 * @param msg
	 * @param gmo
	 * @throws MQException
	 */
	public void getMsg(MQMessage msg, MQGetMessageOptions gmo)
			throws MQException {	
		getQueue().get(msg, gmo);
	}
	
}
